// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Xml;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS.Parsers
{
	internal class OpmlFeedParser : ParserBase
	{
		private readonly Feed m_Feed = new Feed(Guid.NewGuid(),null);
		readonly Dictionary<int,Feed> m_Parents = new Dictionary<int, Feed>();

		public OpmlFeedParser()
		{
			m_handlers["opml"] = OpmlHandler;
			m_handlers["title"] = delegate(XmlReader aReader) { m_Feed.Name = ReadElementString(aReader); };
			m_handlers["dateModified"] = PubDateHandler;
			m_handlers["outline"] = ItemHandler;
		}

		private void ItemHandler(XmlReader aReader)
		{
			string title = string.Empty, text = string.Empty, url = string.Empty;

			AttributeCollection att = ReadAttributes(aReader);

			if (att.ContainsKey("xmlUrl"))
				url = att["xmlUrl"];

			if (att.ContainsKey("xmlURL"))
				url = att["xmlURL"];

			if (att.ContainsKey("url"))
				url = att["url"];

			if (att.ContainsKey("URL"))
				url = att["URL"];

			if (att.ContainsKey("title"))
				title = att["title"];

			if (att.ContainsKey("text"))
				text = att["text"];

			Feed parent = GetParentFeed(aReader);
			Feed item = new Feed(Guid.NewGuid(),parent);
			parent.Feeds.Add(item);
			m_Parents[aReader.Depth] = item;

			if (!string.IsNullOrEmpty(title))
				item.Name = title;
			else if (!string.IsNullOrEmpty(text))
				item.Name = text;
			else
				item.Name = "<No name>";

			item.FeedUrl = url;
		}

		Feed GetParentFeed(XmlReader aReader)
		{
			if (m_Parents.ContainsKey(aReader.Depth - 1))
				return m_Parents[aReader.Depth - 1];
			else
				return m_Feed;
		}

		private static void OpmlHandler(XmlReader aReader)
		{
			/*AttributeCollection att = ReadAttributes(aReader);
			if (!att.ContainsKey("version") || string.IsNullOrEmpty(att["version"]))
				throw new InvalidOperationException("The feed is not OPML 2.0 Feed!");*/
		}

		private void PubDateHandler(XmlReader aReader)
		{
			if (!m_Feed.LastModifiedDate.HasValue)
			{
				string orgDate;
				m_Feed.LastModifiedDate = ReadDateTime(aReader, out orgDate);
			}
		}

		public Feed Feed
		{
			get { return m_Feed; }
		}
	}
}